<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class NewsCarouselController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
	 	$userName = I('param.userName');
		$sex = I('param.sex');
		if($userName){
			$where = "&userName=$userName"; 
			
		}
		if($sex !== '全部'){
			$where .= "&sex=$sex";
				
		}
		$trtotal = $this->curl_http(C('news_url').'banner/getBannerListA?token='.$_SESSION["TOKEN"].'&curPage=1&pageSize=1'.$where);
		$page = $this->page($trtotal['ReturnData']['page']['totalCount'],20);
		$result = $this->curl_http(C('news_url').'banner/getBannerListA?token='.$_SESSION["TOKEN"]."&curPage=".$page->Current_page."&pageSize=".$page->Page_size.$where);
		$this->assign("data", $result['ReturnData']['data']);
		$this->assign("page", $page->show('Admin')); 
		$this->display();
	}
    function add(){
//        $newsClass = $this->curl_http(C('news_url').'newsCategory/getNewsCategoryList?token='.$_SESSION["TOKEN"]);
        $this->assign("newsClass");
        $this->display();
    }
    function add_post(){
        $params = array(
            't' => I("param.t", ''),
            'bT' => I("param.bT", -1),
            'iU' => I("param.iU", 0),
            'cI' => I("param.cI", -1),
            'sm' => I("param.sm", ''),
            'lU' => I("param.lU", ''),
            'sI' => I("param.sI", 0),
        );
        if (empty($params['t'])) {
            $this->error("新闻标题不能为空！");
        }
        if ($params['bT'] <= 0) {
            $this->error("请选择类型！");
        }
        if (!$params['iU']) {
            $this->error("请上传新闻图片！");
        } else {
            $params['iU'] = "http://{$_SERVER['HTTP_HOST']}" . $params['iU'];
        }
        if ($params['newsType'] == -1) {
            $this->error("请选择新闻类型！");
        }
        if (!$params['lU']) {
            $this->error("请添加url！");
        }
        $fields = '&t=' . $params['t'] . '&bT=' . $params['bT'] . '&iU=' . $params['iU'] . '&cI=' . $params['cI'] . '&lU=' . $params['lU']. '&sI=' . $params['sI'] . '&sm=' . $params['sm'];
        $result = $this->curl_http(C('news_url').'banner/addBanner?token='.$_SESSION["TOKEN"], $fields);
        if($result['Success']){
            $this->success("添加成功！", U("Admin/NewsCarousel/index"));
        }else{
            return $this->error($result['Message']);
        }
        
    }
    function edit(){
        $params = array(
            'bId' => I("param.bId", ''),
            't' => I("param.t", ''),
            'bT' => I("param.bT", -1),
            'iU' => I("param.iU", 0),
            'cI' => I("param.cI", -1),
            'sm' => I("param.sm", ''),
            'lU' => I("param.lU", ''),
            'sI' => I("param.sI", 0),
        );
        $this->assign('data',$params);
        $newsClass = $this->curl_http(C('news_url').'newsCategory/getNewsCategoryList?token='.$_SESSION["TOKEN"]);
        $this->assign("newsClass", $newsClass['ReturnData']['data']);
        $this->display();
    }
    function edit_post(){
         $params = array(
            'bId' => I("param.bId", ''),
            't' => I("param.t", ''),
            'bT' => I("param.bT", -1),
            'iU' => I("param.iU", 0),
            'cI' => I("param.cI", -1),
            'sm' => I("param.sm", ''),
            'lU' => I("param.lU", ''),
            'sI' => I("param.sI", 0),
        );
        if (empty($params['bId'])) {
            $this->error("id不能为空！");
        }
        if (empty($params['t'])) {
            $this->error("新闻标题不能为空！");
        }
        if ($params['bT'] <= 0) {
            $this->error("请选择类型！");
        }
        if (!$params['iU']) {
            $this->error("请上传新闻图片！");
        } else {
            if((strpos($params['iU'], 'http://') !== false) or (strpos($params['iU'], 'https://') !== false)){
                $params['iU'] =  $params['iU'];
            }else{
             $params['iU'] = "http://{$_SERVER['HTTP_HOST']}" . $params['iU'];
            }
        }
        if ($params['newsType'] == -1) {
            $this->error("请选择新闻类型！");
        }
        if (!$params['lU']) {
            $this->error("请添加url！");
        }
        $fields = '&t=' . $params['t'] . '&bT=' . $params['bT'] . '&iU=' . $params['iU'] . '&cI=' . $params['cI'] . '&lU=' . $params['lU']. '&sI=' . $params['sI'] . '&sm=' . $params['sm'].'&bId='.$params['bId'];
        $result = $this->curl_http(C('news_url').'banner/modifyBanner?token='.$_SESSION["TOKEN"], $fields);
        if($result['Success']){
            $this->success("修改成功！", U("Admin/NewsCarousel/index"));
        }else{
            return $this->error($result['Message']);
        }
    }
    function del(){
        $bId = I("param.bId",0);
        $result = $this->curl_http(C('news_url').'banner/delBanner?token='.$_SESSION["TOKEN"].'&bannerIds='.$bId);
        if($result['Success']){
            $this->success("删除成功！", U("Admin/NewsCarousel/index"));
        }else{
            return $this->error($result['Message']);
        }
    }
    function listorders(){
        if(!empty($_POST['listorders'])){
            foreach($_POST['listorders'] as $key=>$val){
                
                $result = $this->curl_http(C('news_url').'banner/modifyBanner?token='.$_SESSION["TOKEN"].'&bId='.$key.'&sI='.$val);
                if($result['ResultCode'] != '0000'){
                    $this->error("排序更新失败！");
                }
            }
           $this->success("排序更新成功！");
        }
        
        
    }
	
}